<?php

namespace App\Library\Rules;

use Illuminate\Contracts\Validation\Rule;

class MediaImgRule extends BaseRule implements Rule
{
    private string $errorMessage = '媒体格式错误';

    /**
     * Desc:
     * @param string $attribute
     * @param mixed $value
     * @return boolean
     */
    public function passes($attribute, $value): bool
    {
        $this->errorMessage = $attribute . ' ' . $this->errorMessage;
        if (!preg_match(__('com.serialNumberReg'), $value)) return false;
        $mimeType = getCacheUploadFile($value);
        if (is_null($mimeType)) {
            $this->errorMessage = $attribute . ' 媒体不存在:' . $value;
            return false;
        } else if (!in_array($mimeType, __('mime_type.img'))) return false;
        else return true;
    }

    /**
     * Desc:
     * @return string
     */
    public function message(): string
    {
        return $this->errorMessage;
    }
}
